package com.qiwx.pigeon.app;

import android.content.Context;

import androidx.multidex.MultiDex;
import androidx.multidex.MultiDexApplication;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.backends.okhttp3.OkHttpImagePipelineConfigFactory;
import com.facebook.imagepipeline.core.ImagePipelineConfig;
import com.github.pwittchen.reactivenetwork.library.ConnectivityStatus;
import com.github.pwittchen.reactivenetwork.library.ReactiveNetwork;
import com.jzg.lib.crash.JzgCrashHandler;
import com.qiwx.pigeon.BuildConfig;
import com.qiwx.pigeon.Utils.RxThreadUtil;
import com.qiwx.pigeon.Utils.SharedPreferencesUtil;
import com.qiwx.pigeon.data.network.CustomerOkHttpClient;
import com.qiwx.pigeon.data.network.ApiService;
import com.qiwx.pigeon.data.vo.EmployBean;
import com.qiwx.pigeon.data.vo.WareHouseBean;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @description
 * @Author qiwx
 * @Date 2020-03-23 11:27
 **/
public class AppContext extends MultiDexApplication {

    private static AppContext instance;
    public static boolean isGoTimeByMinute = false;//是否按分钟计算
    private static final String API = BuildConfig.BASE_URL;
    public static boolean isNetWork = true;//true有网false没网
    public static int isWifi = 0;
    public static List<WareHouseBean> houses = new ArrayList();

    public static EmployBean employBean = null;


    @Override
    public void onCreate() {
        super.onCreate();
        MultiDex.install(getApplicationContext());
        instance = this;
        initFresco();
        initNet();
        //initDB();
        // initPeriodDefault();
    }

    public static AppContext getContext() {
        return instance;
    }

    private void initFresco() {
        OkHttpClient frescoOkHttpClient = CustomerOkHttpClient.getFrescoOkHttpClient();
        ImagePipelineConfig config = OkHttpImagePipelineConfigFactory
                .newBuilder(getApplicationContext(), frescoOkHttpClient)
                .build();
        Fresco.initialize(getApplicationContext(), config);
        JzgCrashHandler.getInstance().init(this);

    }

    //初始化数据库
    private void initDB() {
        LitePal.initialize(this);
    }

    //从配置文件里读取鸽笼各个周期默认值
    private void initPeriodDefault() {
        SharedPreferencesUtil.initPeriod(this);
        isGoTimeByMinute = SharedPreferencesUtil.getIsGoTimeByMinute(this);

    }

    public void initNet() {
        ReactiveNetwork reactiveNetwork = new ReactiveNetwork();
        reactiveNetwork.observeConnectivity(this)
                .compose(RxThreadUtil.normalSchedulers())
                .subscribe(connectivityStatus -> {
                    isNetWork = !connectivityStatus.toString().equals(ConnectivityStatus.OFFLINE.toString()) || connectivityStatus.toString().equals(ConnectivityStatus.WIFI_CONNECTED_HAS_NO_INTERNET.toString());
                    if (connectivityStatus.toString().equals(ConnectivityStatus.WIFI_CONNECTED.toString()) && connectivityStatus.toString().equals(ConnectivityStatus.WIFI_CONNECTED_HAS_NO_INTERNET.toString())) {
                        //连接了无线,且无线不能用
                        isWifi = 1;
                    } else if (connectivityStatus.toString().equals(ConnectivityStatus.OFFLINE.toString())) {
                        //网络不可用
                        isWifi = 1;
                    } else if (connectivityStatus.toString().equals(ConnectivityStatus.MOBILE_CONNECTED.toString())) {
                        //连接了移动网络
                        isWifi = 2;
                    } else {
                        isWifi = 0;
                    }
                });
    }

    public static EmployBean getEmployBean(Context context) {
        if (employBean == null) {
            employBean = SharedPreferencesUtil.getEmployBean(context);
        }
        return employBean;
    }

    //获取鸽场id
    public static int getFarmId(Context context) {
        if (employBean == null) {
            employBean = SharedPreferencesUtil.getEmployBean(context);
        }
        if (employBean != null) {
            return employBean.getF_id();
        }
        return 0;
    }


}
